from ninja import Schema, Field, FilterSchema, ModelSchema
from typing import Optional
from django.db.models import Q

from models.models import User, PermissionGroup, ManagerUser


class UserFiltersSchema(FilterSchema):
    """用户筛选"""
    user: Optional[str] = None
    invite_id: Optional[str] = Field(None, q="invite_id__icontains", description="邀请人")
    user_type: Optional[int] = Field(None, description="用户身份")
    agent_type: Optional[int] = Field(None, description="代理身份")
    region: Optional[str] = None
    create_time: Optional[dict] = None
    water_card_number: Optional[dict] = None
    water_droplets_number: Optional[dict] = None

    def filter_user(self, value) -> Q:
        return Q(openid__icontains=value) | Q(nickname__icontains=value) | Q(phone_number__icontains=value)

    def filter_region(self, value) -> Q:
        return Q(country__icontains=value) | Q(province__icontains=value) | Q(city__icontains=value)

    def filter_water_card_number(self, value: dict) -> Q:
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(water_card_number__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(water_card_number__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(water_card_number__gt=value.get('start'), water_card_number__lt=value.get('end'))
        return Q()

    def filter_water_droplets_number(self, value: dict) -> Q:
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(water_droplets_number__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(water_droplets_number__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(water_droplets_number__gt=value.get('start'), water_droplets_number__lt=value.get('end'))
        return Q()

    def filter_create_time(self, value: dict):
        if value is None:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(create_time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(create_time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(create_time__gt=value.get('start'), create_time__lt=value.get('end'))
        return Q()

    class Config:
        fields = "__all__"


class VipIdentitySettingsSchema(Schema):
    """会员身份设置"""
    user_id: int = Field(default=0, description="身份ID")
    vip_id: int = Field(default=0, description="身份等级")
    day: int = Field(default=0, description="天数")
    note: str = Field(default="", description="备注")


class AgentIdentitySettingsSchema(Schema):
    """代理身份设置"""
    user_id: int = Field(default=0, description="身份ID")
    agent_id: int = Field(default=0, description="代理身份等级")
    water_card_number: int = Field(default=0, description="水卡数量")
    note: str = Field(default="", description="备注")


class ManagerLoginSchema(Schema):
    """管理员登录"""
    username: str = Field(default="", description="用户名")
    password: str = Field(default="", description="密码")


class UsersResponseSchema(ModelSchema):
    class Config:
        model = User
        model_fields = "__all__"


class ManagerUsersResponseSchema(ModelSchema):
    class Config:
        model = ManagerUser
        model_fields = "__all__"


class PermissionGroupResponseSchema(ModelSchema):
    class Config:
        model = PermissionGroup
        model_fields = "__all__"


class PermissionGroupSchema(Schema):
    """权限组"""
    name: str = Field(default="", description="权限组名称")
    description: str = Field(default="", description="描述")


class PermissionGrantSchema(Schema):
    """权限组"""
    api: list = Field(default=[], description="授权接口")
    menu: list = Field(default=[], description="授权菜单")


class ManagerUserCreateSchema(Schema):
    """管理员用户"""
    username: str = Field(default="", description="用户名")
    alias: str = Field(default="", description="别名")
    password: str = Field(default="", description="密码")
    permission_group: int = Field(default=None, description="权限组")
    mobile: str = Field(default="", description="手机号")
    email: str = Field(default="", description="邮箱")


class ManagerUserLoginSchema(Schema):
    """管理员用户登录"""
    username: str = Field(default="", description="用户名")
    password: str = Field(default="", description="密码，MD5加密后传输")


class ManagerUserChangePasswordSchema(Schema):
    """管理员用户修改密码"""
    old_password: str = Field(default="", description="旧密码，MD5加密后传输")
    new_password: str = Field(default="", description="新密码，MD5加密后传输")


class AddMenuSchema(Schema):
    """添加菜单"""
    name: str = Field(default="", description="菜单名称")
    link: str = Field(default="", description="菜单路径")
    icon: str = Field(default="", description="菜单图标")
    father_id: int = Field(default=None, description="父级菜单ID")
    apis: list = Field(default=[], description="接口列表")


class UploadFileSchema(Schema):
    type: str = Field(default="", description="类型,商品为good,广告为ad,知识库为knowledge")


class OrderFilterByRegionFilterSchema(FilterSchema):
    region: Optional[str] = None

    def filter_region(self, value: str) -> Q:
        return Q(address__province_name__icontains=value) | Q(address__city_name__icontains=value) | Q(
            address__district_name__icontains=value)

    class Config:
        fields = "__all__"
